package com.example.live.dialog
import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialog
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.bw.live.view.RedCircleCountDownView
import com.example.live.R
import com.example.live.adapter.RobAdapter
import com.example.live.entity.RobEntity

/**
 * 自定义红包对话框
 */
class RedEnvelopeDialog: AppCompatDialog {
    constructor(context: Context): super(context)
    constructor(context: Context, themeResId: Int): super(context, themeResId)
    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?): super(context, cancelable, cancelListener)
    private var time = 0L
    fun setTime(time: Long) {
        this.time = time
    }
    private val list = mutableListOf<RobEntity>()
    private val robAdapter = RobAdapter()

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        list.add(RobEntity("张三1",50))
        list.add(RobEntity("张三2",40))
        list.add(RobEntity("张三3",30))
        list.add(RobEntity("张三4",20))
        list.add(RobEntity("张三5",20))
        list.add(RobEntity("张三6",20))
        list.add(RobEntity("张三7",5))
        list.add(RobEntity("张三8",5))
        list.add(RobEntity("张三9",5))
        list.add(RobEntity("张三10",5))
        robAdapter.data.addAll(list)
        //加载布局
        setContentView(R.layout.red_envelope_dialog_layout)
        val downView = findViewById<RedCircleCountDownView>(R.id.rcd)
        downView?.setCountDownTime(time.toInt())
        downView?.startCountDown()
        val prize = findViewById<ConstraintLayout>(R.id.rob_prize_layout)
        val bottomImage = findViewById<ImageView>(R.id.rob_red_envelope_bottom)
        val bottomText = findViewById<TextView>(R.id.rob_look_fortune)
        val robLayout = findViewById<LinearLayout>(R.id.rob_layout)
        val listLayout = findViewById<LinearLayout>(R.id.list_layout)
        val moneyLayout = findViewById<LinearLayout>(R.id.money_layout)
        val robRv = findViewById<RecyclerView>(R.id.rob_rv)
        downView?.setOnClickListener {
            if(downView.isFinish){
                prize?.visibility = View.VISIBLE
                bottomImage?.visibility = View.VISIBLE
                bottomText?.visibility = View.VISIBLE
            }else{
                ToastUtils.showShort("还未开抢")
            }
        }
        robRv?.adapter = robAdapter
        robRv?.layoutManager = LinearLayoutManager(context)
        bottomText?.setOnClickListener {
            robLayout?.visibility = View.GONE
            listLayout?.visibility = View.VISIBLE
            bottomImage?.visibility = View.GONE
            moneyLayout?.visibility = View.GONE
            downView?.visibility = View.GONE
            bottomText.visibility = View.GONE
        }

        //取消事件
        findViewById<ImageView>(R.id.red_close)?.setOnClickListener{
            cancel()
            downView?.cancelCountDown()
        }

        val windowParams: WindowManager.LayoutParams = window?.attributes!!
        windowParams.apply {
            dimAmount = 0.5f //设置灰暗度
            width = WindowManager.LayoutParams.WRAP_CONTENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
        }
        window?.attributes = windowParams
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setCancelable(true)
        setCanceledOnTouchOutside(true)
    }

}